<template>
	<view class="zfbOrder">
		<view class="header" @click="click">确认订单 <text class="close" @click="close">+</text> </view>
		<view class="bichu">本次开合,必出史诗</view>
		<view class="orderInfo">
			<image class="img" :src="data.cover_image"></image>
			<view class="desc">
				<view class="name">{{data.activity_describe}}
					<text class="tag">首抽仅￥0.01</text>
				</view>
				<view class="price">
					<view class="num">
						<text class="unit">￥</text>
						{{data.first_blood}}
					</view>
					<view class="count">数量 x 1</view>
				</view>
			</view>
		</view>
		<view class="payWay">
			<label class="way">
				<view class="l">
					<image class="icon"></image>
					<text class="text">支付宝</text>
				</view>
				<checkbox class="checkbox" :checked="true" />
			</label>
		</view>
		<view class="footer">
			<view class="total">
				<view class="l">合计：￥{{data.first_blood}}，
					<text class="span">共减￥0</text>
				</view>
				<view class="r" @click="showDetail=!showDetail">明细 <image
						:style="showDetail?'transform:rotateZ(180deg)':''" class="top"
						src="@/static/images/newhone/top.png" mode=""></image>
				</view>
			</view>
			<view class="btn" @click="payBefore">
				立即支付 <text class="count">￥</text>{{data.first_blood}}
			</view>
			<view class="agree" @click="isAgree=!isAgree">
				<image class="agreeImg" v-if="isAgree" src="@/static/images/newhone/check.png"></image>
				<image class="agreeImg" v-else src="@/static/images/newhone/noCheck.png"></image>
				我已满18岁，已阅读并同意
				<text class="text">《支付服务协议》</text>
			</view>
		</view>
		<view class="priceInfo" :style="showDetail?'bottom: 40vh;':'bottom: -80vh;height:0'">
			<view class="header">价格明细 </view>
			<view class="pricetotal">
				<view class="left">商品总价
					<text class="count">共1件</text>
				</view>
				<view class="right">
					<text class="unit">￥</text>
					{{data.first_blood}}
				</view>
			</view>
			<view class="youhui">
				<view class="left"> <text class="hui">惠</text> 限时折扣盲盒</view>
				<view class="right">-￥0</view>
			</view>
			<view class="heji">
				<view class="left">合计：</view>
				<view class="right"> <text class="unit">￥</text>{{data.first_blood}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			data: {
				type: Object,
				default: () => {
					return {}
				}
			}
		},
		data() {
			return {
				isAgree: false,
				showDetail: false,
				allowTap: true, //防误触
				payActive: 1
			}
		},
		onLoad() {},
		methods: {
			click() {
				console.log(this.data, 'data');
			},
			close() {
				this.$emit('close')
			},
			payBefore() {
				if (!this.isAgree) {
					uni.showToast({
						title: '请阅读并同意支付服务协议',
						icon: 'none'
					})
					return
				}
				if (!this.allowTap) return
				this.allowTap = false;
				setTimeout(() => {
					this.allowTap = true;
				}, 3000)

				if (this.payActive == 0) {
					//支付类型为余额
					this.$api.indexExtractboxd({
						activity_id: this.activity_id,
						coupon_id,
						balance: this.payableMoney,
						open_method: this.open_method,
						price: '0',
						pay_type: '0',
						box_d: this.timestamp
					}).then(res => {
						console.log("抽取盲盒金额为0: ", res);
						uni.redirectTo({
							url: '/pages/blindbox/blindbox_result?' + this.$url.urlEncode({
								activity_id: this.activity_id,
								price: this.price,
								order_id: res.order_id,
								open_method: this.open_method
							})
						})
					})
				} else {
					//支付类型为支付宝 微信
					this.$api.indexExtractboxb({
						activity_id: this.data.id,
						coupon_id: '',
						balance: '0',
						open_method: 1,
						price: this.data.first_blood,
						pay_type: this.payActive
					}).then(res => {
						console.log('抽取盲盒金额大于0', res);
						this.order_id = res.order_id;
						//支付
						this.goPay(this.payActive, res.pay_info, this.open_method);
					})
				}
			},
			//去支付
			goPay(pay_type, payInfo) {
				uni.showLoading({
					title: '支付中...'
				})
				if (pay_type == 2) {
					uni.getProvider({
						service: 'payment',
						success: (res) => {
							if (res.provider.indexOf('wxpay') != -1) {
								uni.requestPayment({
									provider: "wxpay",
									orderInfo: payInfo,
									success: (res) => {
										console.log('success:' + JSON.stringify(res));
										uni.hideLoading();
										uni.redirectTo({
											url: '/pages/blindbox/blindbox_result?' + this
												.$url.urlEncode({
													activity_id: this.activity_id,
													price: this.price,
													order_id: this.order_id,
													open_method: this.open_method
												})
										})
									},
									fail: (err) => {
										console.log('fail:' + JSON.stringify(err));
										uni.hideLoading();
										uni.showToast({
											title: '支付失败，请重试',
											icon: 'none'
										})
									}
								})
							}
						}
					})
				} else {
					uni.getProvider({
						service: 'payment',
						success: (res) => {
							console.log(res,'sss');
							if (res.provider.indexOf('alipay') != -1) {
								uni.requestPayment({
									"provider": "alipay",
									"orderInfo": payInfo,
									success: (res) => {
										console.log('success:' + JSON.stringify(res));
										uni.hideLoading();
										uni.redirectTo({
											url: '/pages/blindbox/blindbox_result?' + this
												.$url.urlEncode({
													activity_id: this.data.id,
													price: this.data.first_blood,
													order_id: this.order_id,
													open_method: 1
												})
										})
									},
									fail: (err) => {
										console.log('fail:' + JSON.stringify(err));
										uni.hideLoading();
										uni.showToast({
											title: '支付失败，请重试',
											icon: 'none'
										})
									}
								})
							}
						}
					})
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.zfbOrder {
		height: 80vh;
		background-color: #f3f8fb;

		.header {
			width: 100%;
			padding: 20rpx;
			box-sizing: border-box;
			text-align: center;
			font-weight: 600;
			font-size: 32rpx;
			position: relative;
			background-color: #fff;

			.close {
				font-size: 60rpx;
				transform: rotateZ(45deg);
				position: absolute;
				right: 20rpx;
				top: 0rpx;
			}
		}

		.bichu {
			text-align: center;
			padding: 20rpx;
			background-image: url('@/static/images/newhone/bichu.png');
			background-repeat: no-repeat;
			background-size: 100% 100%;
			margin-bottom: 20rpx;
			font-style: italic;
			font-weight: 600;
			letter-spacing: 5rpx;
		}

		.orderInfo {
			width: 95%;
			display: flex;
			align-items: center;
			padding: 20rpx;
			box-sizing: border-box;
			background-color: #fff;
			border-radius: 10rpx;
			margin: auto;

			.img {
				width: 120rpx;
				height: 120rpx;
				margin-right: 20rpx;
				border-radius: 10rpx;
			}

			.desc {
				flex: 1;
				height: 120rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;

				.name {
					font-weight: 600;
					font-size: 30rpx;

					.tag {
						display: inline-block;
						background-color: #fcecea;
						font-size: 12rpx;
						font-weight: 400;
						color: #da0505;
						transform: scale(.8);
						padding: 2rpx 10rpx;
					}
				}

				.price {
					display: flex;
					align-items: center;
					justify-content: space-between;

					.num {
						font-weight: 600;
						font-size: 28rpx;

						.unit {
							font-size: 12rpx;
						}
					}

					.count {
						font-weight: 600;
						color: #999;
					}
				}
			}
		}

		.payWay {
			width: 95%;
			padding: 20rpx;
			box-sizing: border-box;
			background-color: #fff;
			border-radius: 10rpx;
			margin: 30rpx auto;

			.way {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.l {
					display: flex;
					align-items: center;

					.icon {
						width: 40rpx;
						height: 40rpx;
						border-radius: 5rpx;
						margin-right: 20rpx;
						background-image: url('@/static/images/newhone/zfb.png');
						background-repeat: no-repeat;
						background-size: 100% 100%;
					}

					.text {
						font-size: 16rpx;
					}
				}

				.checkbox {
					transform: scale(.7);
				}
			}
		}

		.footer {
			background-color: #fff;
			padding: 20rpx;
			box-sizing: border-box;
			position: absolute;
			left: 0;
			bottom: 0;

			.total {
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 20rpx 0;

				.l {
					.span {
						color: #fe3f34;
					}
				}

				.r {
					color: fe3f34;

					.top {
						width: 20rpx;
						height: 20rpx;
						margin-left: 20rpx;

					}
				}
			}

			.btn {
				width: 95vw;
				margin: auto;
				background-color: #0c60fa;
				color: #fff;
				font-size: 32rpx;
				font-weight: 600;
				padding: 30rpx 0;
				text-align: center;
				border-radius: 10rpx;
				margin: 20rpx auto;

				.count {
					font-size: 18rpx;
					margin: 0 5rpx;
				}
			}

			.agree {
				display: flex;
				align-items: center;
				justify-content: center;
				color: #999;
				font-size: 12rpx;

				.agreeImg {
					width: 25rpx;
					height: 25rpx;
					background-color: #000;
					border-radius: 50%;
					margin-right: 10rpx;
				}

				.text {
					color: #333;
				}
			}
		}

		.priceInfo {
			width: 100%;
			height: 50%;
			display: flex;
			align-items: space-between;
			box-sizing: border-box;
			background-color: #fff;
			border-radius: 10rpx;
			margin: auto;
			flex-direction: column;
			position: absolute;
			left: 0;
			transition: bottom .3s linear;
			background-color: #f3f8fb;
			overflow: hidden;

			.header {
				width: 100%;
				padding: 20rpx;
				box-sizing: border-box;
				text-align: center;
				font-weight: 600;
				font-size: 32rpx;
				position: relative;
				background-color: #fff;
				margin: 0 0 30rpx;

				.close {
					font-size: 60rpx;
					transform: rotateZ(45deg);
					position: absolute;
					right: 20rpx;
					top: 0rpx;
				}
			}

			.pricetotal,
			.youhui,
			.heji {
				width: 95%;
				background-color: #fff;
				display: flex;
				align-items: center;
				justify-content: space-between;
				padding: 10rpx 20rpx;
				margin: 0 auto;

				.left {
					.hui {
						display: inline-block;
						width: 30rpx;
						height: 30rpx;
						border-radius: 5rpx;
						background-color: orangered;
						text-align: center;
						line-height: 30rpx;
						font-size: 12rpx;
						color: #fff;
						margin-right: 10rpx;
					}

					.count {}
				}

				.right {
					font-size: 28rpx;

					.unit {
						font-size: 12rpx;
					}
				}
			}

			.youhui {
				font-size: 12rpx;

				.right {
					color: #fe3f34;
				}
			}

			.pricetotal {
				padding-top: 40rpx;
				border-radius: 10rpx 10rpx 0 0;
			}

			.heji {
				padding-bottom: 40rpx;
				border-radius: 0 0 10rpx 10rpx;
			}
		}
	}
</style>
